package com.sharding.config;

import com.zaxxer.hikari.HikariDataSource;
import io.shardingsphere.core.api.ShardingDataSourceFactory;
import io.shardingsphere.core.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.api.config.TableRuleConfiguration;
import io.shardingsphere.core.api.config.strategy.ComplexShardingStrategyConfiguration;
import io.shardingsphere.core.api.config.strategy.InlineShardingStrategyConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by dongjigong on 2018/6/6.
 *
 * @Email osbornjonny@126.com
 * @Description
 */
@Configuration
public class ShardingJdbcConfig {

    @Bean(name="shardingDataSource")
    public DataSource shardingDataSource() {

        // 配置真实数据源
        Map<String, DataSource> dataSourceMap = new HashMap<>();

        // 配置第一个数据源
        HikariDataSource dataSource1 = new HikariDataSource();
        dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/ds0");
        dataSource1.setUsername("root");
        dataSource1.setPassword("123456");
        dataSourceMap.put("db11834", dataSource1);

        // 配置第二个数据源
        HikariDataSource dataSource2 = new HikariDataSource();
        dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource2.setJdbcUrl("jdbc:mysql://localhost:3306/ds1");
        dataSource2.setUsername("root");
        dataSource2.setPassword("123456");
        dataSourceMap.put("db13655", dataSource2);

        // 配置Order表规则
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
        orderTableRuleConfig.setLogicTable("t_order");
        orderTableRuleConfig.setActualDataNodes("db11834.t_order,db13655.t_order");
        //orderTableRuleConfig.setKeyGeneratorColumnName("order_id");
        TableRuleConfiguration relationTable = new TableRuleConfiguration();
        relationTable.setLogicTable("t_order_item");
        //relationTable.setActualDataNodes("db11834,db13655}");

        // 配置分库 + 分表策略
        orderTableRuleConfig.setDatabaseShardingStrategyConfig
            (new ComplexShardingStrategyConfiguration("school_id", new MyComplexShardingStrategy()));
//        orderTableRuleConfig.setTableShardingStrategyConfig
//            (new InlineShardingStrategyConfiguration("order_id", "t_order${order_id % 2}"));

        // 配置分片规则
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
        shardingRuleConfig.getTableRuleConfigs().add(relationTable);
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig
            (new InlineShardingStrategyConfiguration("member_id", "ds${member_id % 2}"));
        //shardingRuleConfig.setDefaultTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id","t_order${order_id % 2}"));
        // 省略配置order_item表规则...
        // ...

        // 获取数据源对象
        DataSource dataSource =null ;


        try {
            Properties props = new Properties();
            props.setProperty("sql.show", "true");
            dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new ConcurrentHashMap(), props);
            return dataSource;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;

    }

}
